const fs = require('fs');
const path = require('path');

// 读取课程索引文件
const coursesDir = path.join(__dirname, '../resources/courses');
const coursesIndexFile = path.join(coursesDir, 'index.json');

console.log('=== Course Index Analysis ===');
try {
  const indexContent = fs.readFileSync(coursesIndexFile, 'utf8');
  const coursesIndex = JSON.parse(indexContent);
  console.log('Courses Index:', coursesIndex);
} catch (error) {
  console.error('Error reading courses index:', error);
}

// 检查数据库中user_course_progress表的数据
const sqlite3 = require('../server/node_modules/sqlite3').verbose();

const dbPath = path.join(__dirname, '../resources/db/db.database');
console.log('\nDatabase path:', dbPath);

const db = new sqlite3.Database(dbPath);

console.log('\n=== User Course Progress Data ===');
db.serialize(() => {
  db.all("SELECT * FROM user_course_progress ORDER BY updated_at DESC", (err, rows) => {
    if (err) {
      console.error('Error:', err);
    } else {
      console.log('User course progress rows:', rows);
      
      // 尝试将user_course_progress中的course_id与index.json中的id匹配
      try {
        const indexContent = fs.readFileSync(coursesIndexFile, 'utf8');
        const coursesIndex = JSON.parse(indexContent);
        
        console.log('\n=== Matching Course Titles ===');
        rows.forEach(row => {
          const course = coursesIndex.find(c => c.id === row.course_id);
          if (course) {
            console.log(`Course ID ${row.course_id}: ${course.title}`);
          } else {
            console.log(`Course ID ${row.course_id}: Title not found`);
          }
        });
      } catch (readError) {
        console.error('Error reading courses index for matching:', readError);
      }
    }
    
    db.close();
  });
});